<?php defined('SYSPATH') or die('No direct script access.');

class Model_Register {
         
    public function reg($email, $regcodevalue, $role )
    {
        $regcode = new Model_Regcode();
        $myuser = new Model_Myuser();
        $regcode -> code = $regcodevalue;
        
        try
        {
            $regcode ->check();
        }
        catch(ORM_Validation_Exception $e)
        {
            $this -> errors = $e -> errors('validation');
            return FALSE;
        }
        
        //Создаем пользователя
        $myuser -> username = $email;
        $myuser -> email = $email;
        $myuser -> password = "12345";
        
        try
        {
            $myuser -> save();
            
            //Узнаем id созданного  пользователя
            $usertemp = ORM:: factory('user',array ('username'=>$email));
            $adduserid = $usertemp -> id;
                        
            //Создаем роли
            $addrole = new Model_Addrole();
            $addrole -> user_id = $adduserid;
            $addrole -> role_id = $role;
            $addrole -> save();         
                   
            return TRUE;
        }
        catch (ORM_Validation_Exception $e)
        {
            $this -> errors = $e -> errors('validation');
            return FALSE;
        }
    }  
}
    
   